package com.machine.filling.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.Log;

/**
 * Created by sperfan on 2017/11/23.
 *
 * @描述: 6.0运行时权限工具类
 */

public class PermissionUtil {
    private static final String TAG = "PermissionUtil";

    /**
     * intent 跳转 startActivityForResult
     */
    public static final int ACTIVITY_REQ_SETTING = 20001;

    private PermissionUtil() {
    }
    private static PermissionUtil instance;

    public static PermissionUtil getInstance(){
        if (instance == null){
            instance = new PermissionUtil();
        }
        return instance;
    }

    /**
     * 检查是否拥有权限
     *
     * @param activity
     * @param permission     权限名称
     * @param permission_req 权限申请的requestCode
     * @return
     */
    public boolean checkPermissions(Activity activity, String permission, int permission_req,@NonNull PermissionListener permissionListener) {
        this.permissionListener = permissionListener;
        if (ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED) {
            permissionListener.permissionAllowed();
            return true;
        } else {
            String[] storgePermission = {permission};
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                activity.requestPermissions(storgePermission, permission_req);
            } else {
//                PermissionUtil.goToSetPermission(activity, activity.getResources().getString(R.string.notice_premission_external_storage), ACTIVITY_REQ_SETTING);
                Log.i(TAG, "!!!!!!!!!!!!!!!!!!!!Build_SDK < M=23");
            }
            return false;
        }
    }

    /**
     * 去 设置中 手动设置权限
     *
     * @param activity
     * @param niticeMessage  权限申请时的 提示信息
     */
    public void goToSetPermission(final Activity activity, String niticeMessage) {
        new AlertDialog.Builder(activity)
                .setTitle("权限申请")
                .setMessage(niticeMessage)
                .setPositiveButton("去开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                        intent.setData(uri);
                        activity.startActivityForResult(intent, ACTIVITY_REQ_SETTING);
                    }
                })
                .setCancelable(false)
                .show();
    }

    /**
     * 对应activity的 onRequestPermissionsResult
     * @param requestCode
     * @param permissions
     * @param grantResults
     * @param permission_req  权限申请时的请求码
     */
    public void onRequestPermissionResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults, int permission_req) {
        if (requestCode == permission_req) {
            boolean isAllow = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    isAllow = false;
                    break;
                }
            }
            if (isAllow) {
                permissionListener.permissionAllowed();
            } else {
                permissionListener.goSettingPermission();
            }
        }
    }

    /**
     * 对应activity的onActivityResult
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ACTIVITY_REQ_SETTING && resultCode == Activity.RESULT_OK) {
            if (resultCode == Activity.RESULT_OK) {
                permissionListener.permissionAllowed();
            }else {
                permissionListener.goSettingPermission();
            }
        }
    }

    private PermissionListener permissionListener;

    public interface PermissionListener {

        /**
         * 权限申请成功后的 操作
         */
        void permissionAllowed();

        /**
         * 去"设置" 开启权限
         */
        void goSettingPermission();

    }



}
